﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML dir=ltr XMLNS:MSHelp = "http://msdn.microsoft.com/mshelp" xmlns:ddue = 
"http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink = 
"http://www.w3.org/1999/xlink" xmlns:tool = "http://www.microsoft.com/tooltip" 
XMLNS:[default] http://ddue.schemas.microsoft.com/authoring/2003/5 = 
"http://ddue.schemas.microsoft.com/authoring/2003/5"><HEAD><TITLE>Direct Connection Sample</TITLE>
<META content="text/html; CHARSET=utf-8" http-equiv=Content-Type></META>
<META name=save content=history></META><LINK rel=stylesheet type=text/css 
href="../../../../../CommonFiles/Classic.css"></LINK>

<META name=GENERATOR content="MSHTML 8.00.6001.18783"></HEAD>
<BODY><INPUT id=userDataCache class=userDataStyle type=hidden></INPUT><INPUT 
id=hiddenScrollOffset type=hidden></INPUT><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=dropDownImage 
src="../../../../../../Common/Html/drpdown.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=dropDownHoverImage 
src="../../../../../../Common/Html/drpdown_orange.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=copyImage 
src="../../../../../../Common/Html/copycode.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=copyHoverImage 
src="../../../../../../Common/Html/copycodeHighlight.gif"></IMG>
<DIV id=header>
<TABLE id=topTable width="100%">
  <TBODY>
  <TR id=headerTableRow1>
    <TD align=left><SPAN id=runningHeaderText></SPAN></TD></TR>
  <TR id=headerTableRow2>
    <TD align=left><SPAN id=nsrTitle>Direct Connection Sample</SPAN></TD></TR>
  <TR id=headerTableRow3>
    <TD></TD></TR></TBODY></TABLE>
</DIV>
<DIV id=mainSection>
<DIV id=mainBody>
<DIV id=allHistory class=saveHistory onload="loadAll()" 
onsave="saveAll()"></DIV>
<P>This sample demonstrates how to configure the <B>NetTcpRelayBinding</B> 
binding to support the Hybrid connection mode which first establishes a 
relayed connection, and if possible, switches automatically to a direct 
connection between a client and a service. </P>
<H2 class=heading>Prerequisites</H2>
<DIV id=sectionSection0 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">If you haven't already done so, read the release notes document that 
explains how to sign up for a Windows Azure account and how to 
configure your environment.</P></content></DIV>
<H2 class=heading>Service</H2>
<DIV id=sectionSection1 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">In general, there are four steps to building a Service Bus 
service:</P>
<OL class=ordered xmlns="">
  <LI>Define a contract.<BR><BR>
  <LI>Implement the contract in a service.<BR><BR>
  <LI>Define endpoints for the service.<BR><BR>
  <LI>Host the service.<BR><BR></LI></OL>
<P xmlns="">In this sample, the service project defines 
<CODE>HelloService</CODE> and a simple contract named 
<CODE>IHelloContract</CODE>:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>[ServiceContract(Name = "IHelloContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public interface IHelloContract
{
    [OperationContract(IsOneWay = true)]
    void Hello(string text);
}</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">The service implements this contract in the 
<CODE>HelloService</CODE> class.</P>
<P xmlns="">The endpoints for this service are defined in the application 
configuration file. Specifically, the following endpoint is defined:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>&lt;service name="Microsoft.ServiceBus.Samples.HelloService"&gt;
  &lt;endpoint name="RelayEndpoint"
            contract="Microsoft.ServiceBus.Samples.IHelloContract"
            binding="netTcpRelayBinding"
            bindingConfiguration="default" 
            address="" /&gt;
&lt;/service&gt;</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">This endpoint is configured to use a binding of type 
<B>NetTcpRelayBinding</B>. It references a binding configuration called 
"default" and specifies that the connection mode is "Hybrid."</P>
<P xmlns="">The Hybrid connection mode first establishes a relayed connection, 
and if possible, switches automatically to a direct connection between a client 
and service.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>&lt;bindings&gt;
  &lt;netTcpRelayBinding&gt;
    &lt;binding name="default" connectionMode="Hybrid" /&gt;
  &lt;/netTcpRelayBinding&gt;
&lt;/bindings&gt;

</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>

<H2 class=heading>Client</H2>
<P xmlns="">The client project defines <CODE>HelloClient</CODE>. In the 
application configuration file, the client is configured with the following 
endpoint:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>&lt;client&gt;
  &lt;endpoint name="RelayEndpoint"
            contract="Microsoft.ServiceBus.Samples.IHelloContract"
            binding="netTcpRelayBinding"
            bindingConfiguration="default" /&gt;
&lt;/client&gt;</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">In the code, an endpoint is opened.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "HelloService");
ChannelFactory&lt;IHelloChannel&gt; channelFactory = new ChannelFactory&lt;IHelloChannel&gt;("RelayEndpoint", new EndpointAddress(serviceUri));
channelFactory.Endpoint.Behaviors.Add(relayCredentials);
IHelloChannel channel = channelFactory.CreateChannel();
channel.Open();</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">After the <CODE>ChannelFactory</CODE> has been created, the client 
application creates a channel to the service and then proceeds to interact with 
it by calling <CODE>channel.Hello("Hello")</CODE> numerous times and calculating 
the time between each message response.</P>
<P xmlns="">When a direct connection is established, throughput is significantly increased</P></content></DIV>
<H2 class=heading>Building and Running the Sample</H2>
<DIV id=sectionSection2 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">After building the solution, perform the following steps to run the 
application:</P>
<OL class=ordered xmlns="">
  <LI>From a command prompt, run the service (Service\bin\Debug\Service.exe). 
  <BR><BR>
  <LI>When prompted, enter your service namespace, issuer name and issuer key. At this point, the 
  service should indicate that it is listening at the configured 
address.<BR><BR>
  <LI>In another command prompt window, run the client 
  (Client\bin\Debug\Client.exe).<BR><BR>
  <LI>You will be prompted for the service namespace, issuer name and issuer key.<BR><BR>
  <LI>When finished, press ENTER to exit the client and the service.<BR><BR></LI></OL>
<P xmlns="">If a direct connection is established, you will see an increased 
number of sent messages.</P>
				</content></DIV><!--[if gte IE 5]><tool:tip 
avoidmouse="false" element="languageFilterToolTip"></tool:tip><![endif]--></DIV>
    <hr /> 
    Did you find this information useful?
    <a href="http://go.microsoft.com/fwlink/?LinkID=155664">
        Please send your suggestions and comments about the documentation.

    </a></P></DIV></BODY></HTML>
